package com.sgb.kjwl.view.ui.activity.enterprise;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.google.gson.reflect.TypeToken;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.ActivityPublishServiceCityBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.widget.TwoLevelMultiAreaWindow;
import com.sgb.kjwl.viewmodel.enterprise.PublishServiceCityActivityModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Time:2021/4/16
 * Author:黄淋
 * Description:发布的服务城市
 */
public class PublishServiceCityActivity extends AppActivity {
    ActivityPublishServiceCityBinding mBinding;

    private TwoLevelMultiAreaWindow mCityPop;//筛选服务城市窗口
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> mCityDatas;//服务城市数据
    private List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> allList;
    @Inject
    PublishServiceCityActivityModel mModel;

    private TagAdapter cityAdapter;
    private TagAdapter cityDeleteAdapter;
    private TagFlowLayout flowLayout;
    private ArrayList<BaseAreaEntity> choosenCityList;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder()
                .appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    public static void start(Activity activity, ArrayList<BaseAreaEntity> list, int requestCode) {
        Intent intent = new Intent(activity, PublishServiceCityActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, list);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_publish_service_city);

        mBinding.tvAdd.setOnClickListener(this);
        mBinding.ivDelete.setOnClickListener(this);
        mBinding.tvDeleteAll.setOnClickListener(this);
        mBinding.tvComplete.setOnClickListener(this);
        mBinding.tvConfirm.setOnClickListener(this);
        flowLayout = mBinding.getRoot().findViewById(R.id.city_list);
        initWindow();
    }

    @Override
    protected void init() {
        initData();
        mModel.ldServiceCity.observe(this, this::showCityPopWindow);
    }

    private void initData() {
        if ((ArrayList<BaseAreaEntity>) getIntent().getSerializableExtra(RouterKey.DATA) != null) {
            choosenCityList = (ArrayList<BaseAreaEntity>) getIntent().getSerializableExtra(RouterKey.DATA);
        } else {
            choosenCityList = new ArrayList<>();
        }
        if (choosenCityList != null && choosenCityList.size() == 20) {
            mBinding.tvAdd.setVisibility(View.GONE);
        }
        if (choosenCityList != null && choosenCityList.size() > 0) {
            flowLayout.setVisibility(View.VISIBLE);
        }
        cityAdapter = new TagAdapter<BaseAreaEntity>(choosenCityList) {
            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, BaseAreaEntity entity) {
                final LayoutInflater mInflater = LayoutInflater.from(PublishServiceCityActivity.this);
                LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.item_service_city, flowLayout, false);
                TextView tv = layout.findViewById(R.id.tv_info);
                tv.setText(entity.getName());
                return layout;
            }
        };
        flowLayout.setAdapter(cityAdapter);
        cityAdapter.notifyDataChanged();
    }

    private void initWindow() {
        mCityPop = new TwoLevelMultiAreaWindow(this);
        mCityPop.setMemberHeight();
        mCityPop.setShowSimpleName(true);
        mCityPop.setOnlyShowProAndCity(true);
        mCityPop.setThreeListIsGone(true);
        mCityPop.setProvinceMore();
        mCityPop.setCityMore();
        mCityPop.setOnResultClick(new BasePopWindow.OnResultClick() {
            @SuppressLint("ResourceType")
            @Override
            public void result(Object key1, Object key2, Object key3) {
                allList = (List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>) key3;
                flowLayout.setVisibility(View.VISIBLE);
                mBinding.ivDelete.setVisibility(View.VISIBLE);
                mBinding.llDelete.setVisibility(View.GONE);
                if (choosenCityList != null) {
                    choosenCityList.clear();
                    choosenCityList.addAll((ArrayList<BaseAreaEntity>) key1);
                    if (choosenCityList != null && choosenCityList.size() == 20) {
                        mBinding.tvAdd.setVisibility(View.GONE);
                    }
                    cityAdapter = new TagAdapter<BaseAreaEntity>(choosenCityList) {
                        @Override
                        public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, BaseAreaEntity entity) {
                            final LayoutInflater mInflater = LayoutInflater.from(PublishServiceCityActivity.this);
                            LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.item_service_city, flowLayout, false);
                            TextView tv = layout.findViewById(R.id.tv_info);
                            tv.setText(entity.getName());
                            return layout;
                        }
                    };
                    flowLayout.setAdapter(cityAdapter);
                    cityAdapter.notifyDataChanged();
                    flowLayout.setOnTagClickListener(null);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                if (!ClickUtils.isDoubleClick()) {
                    if (mCityDatas == null) {
                        mModel.getServiceCity();
                    } else if (allList != null && allList.size() > 0) {
                        mCityPop.BaseData(allList, false);
                        mCityPop.showPopwindowBottom(mBinding.tvConfirm);
                    } else if (mCityDatas != null && mCityDatas.size() > 0 && choosenCityList != null && choosenCityList.size() > 0) {
                        showCityPopWindow(mCityDatas);
                    } else {
                        mCityPop.showPopwindowBottom(mBinding.tvConfirm);
                    }
                }
                break;
            case R.id.iv_delete://删除
                if (!ClickUtils.isDoubleClick()) {
                    mBinding.ivDelete.setVisibility(View.GONE);
                    mBinding.llDelete.setVisibility(View.VISIBLE);
                    cityDeleteAdapter = new TagAdapter<BaseAreaEntity>(choosenCityList) {
                        @Override
                        public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, BaseAreaEntity entity) {
                            final LayoutInflater mInflater = LayoutInflater.from(PublishServiceCityActivity.this);
                            LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.item_delete_service_city, flowLayout, false);
                            TextView tv = layout.findViewById(R.id.tv_info);
                            tv.setText(entity.getName());
                            return layout;
                        }
                    };
                    flowLayout.setAdapter(cityDeleteAdapter);
                    flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                        @Override
                        public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent) {
                            if (mBinding.tvAdd.getVisibility() == View.GONE) {
                                mBinding.tvAdd.setVisibility(View.VISIBLE);
                            }
                            String name = choosenCityList.get(position).getName();

                            if (allList != null && allList.size() > 0) {
                                if (name.equals("全国")) {
                                    for (int i = 0; i < allList.size(); i++) {
                                        allList.get(i).setSelected(false);
                                        for (int j = 0; j < allList.get(i).getChildren().size(); j++) {
                                            allList.get(i).getChildren().get(j).setSelected(false);
                                        }
                                    }
                                    allList.get(0).setSelected(true);
                                } else {
                                    for (int i = 0; i < allList.size(); i++) {
                                        if (allList.get(i).getName().equals(name)) {
                                            allList.get(i).setSelected(false);
                                            for (int a = 0; a < allList.get(i).getChildren().size(); a++) {
                                                allList.get(i).getChildren().get(a).setSelected(false);
                                            }
                                            break;
                                        }
                                        for (int j = 0; j < allList.get(i).getChildren().size(); j++) {
                                            if (allList.get(i).getChildren().get(j).getName().equals(name)) {
                                                allList.get(i).getChildren().get(j).setSelected(false);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            choosenCityList.remove(position);
                            cityDeleteAdapter.notifyDataChanged();
                            if (choosenCityList.size() == 0) {
                                flowLayout.setVisibility(View.GONE);
                            }
                            return true;
                        }
                    });
                }
                break;
            case R.id.tv_delete_all://全部删除
                if (!ClickUtils.isDoubleClick()) {
                    if (choosenCityList != null)
                        choosenCityList.clear();
                    if (mBinding.tvAdd.getVisibility() == View.GONE) {
                        mBinding.tvAdd.setVisibility(View.VISIBLE);
                    }
                    flowLayout.setVisibility(View.GONE);
                    cityDeleteAdapter.notifyDataChanged();
                    mBinding.ivDelete.setVisibility(View.VISIBLE);
                    mBinding.llDelete.setVisibility(View.GONE);

                    if (allList != null && allList.size() > 0) {
                        for (int i = 0; i < allList.size(); i++) {
                            allList.get(i).setSelected(false);
                            for (int j = 0; j < allList.get(i).getChildren().size(); j++) {
                                allList.get(i).getChildren().get(j).setSelected(false);
                            }
                        }
                        allList.get(0).setSelected(true);
                    }
                }
                break;
            case R.id.tv_complete://完成
                if (!ClickUtils.isDoubleClick()) {
                    mBinding.ivDelete.setVisibility(View.VISIBLE);
                    mBinding.llDelete.setVisibility(View.GONE);
                    cityAdapter = new TagAdapter<BaseAreaEntity>(choosenCityList) {
                        @Override
                        public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, BaseAreaEntity entity) {
                            final LayoutInflater mInflater = LayoutInflater.from(PublishServiceCityActivity.this);
                            LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.item_service_city, flowLayout, false);
                            TextView tv = layout.findViewById(R.id.tv_info);
                            tv.setText(entity.getName());
                            return layout;
                        }
                    };
                    flowLayout.setAdapter(cityAdapter);
                    flowLayout.setOnTagClickListener(null);
                }
                break;
            case R.id.tv_confirm:
                if (!ClickUtils.isDoubleClick()) {
                    if (choosenCityList != null && choosenCityList.size() > 0) {
                        ArrayList<BaseAreaEntity> cityListShow = new ArrayList<>();
                        if (choosenCityList.size() > 1 &&
                                DisplayUtil.px2dip(this, flowLayout.getChildAt(0).getMeasuredWidth() + flowLayout.getChildAt(1).getMeasuredWidth()) > 250) {
                            cityListShow.add(choosenCityList.get(0));
                        }
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("cityList", (Serializable) choosenCityList);
                        bundle.putSerializable("cityListShow", (Serializable) cityListShow);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        MToast.showToast(PublishServiceCityActivity.this, "请添加服务城市");
                    }
                }
                break;
        }
    }

    public void showCityPopWindow(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> cityDatas) {
        this.mCityDatas = cityDatas;
        if (choosenCityList != null && choosenCityList.size() > 0) {
            String cityListsStr = GsonManager.getInstance().getGson().toJson(cityDatas);
            List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>> list = GsonManager.getInstance().getGson().fromJson(cityListsStr, new TypeToken<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>() {
            }.getType());
            BaseAreaEntity provinceEntity = new BaseAreaEntity();
            provinceEntity.setName("全国");
            provinceEntity.setNo("");
            provinceEntity.setSimpleName("全国");
            provinceEntity.setChildren(new ArrayList<>());
            BaseAreaEntity provinceEntity1 = new BaseAreaEntity();
            provinceEntity1.setName("全国");
            provinceEntity1.setNo("");
            provinceEntity1.setSimpleName("全国");
            provinceEntity1.setSelected(false);
            provinceEntity1.setChildren(new ArrayList<>());
            list.add(0, provinceEntity);
            for (int i = 0; i < list.size(); i++) {
                if (i != 0) {
                    BaseAreaEntity cityEntity = new BaseAreaEntity();
                    cityEntity.setName("全省");
                    cityEntity.setNo("");
                    cityEntity.setSimpleName("全省");
                    cityEntity.setSelected(false);
                    list.get(i).getChildren().add(0, cityEntity);

                    for (int j = 1; j < list.get(i).getChildren().size(); j++) {
                        BaseAreaEntity areaEntity = new BaseAreaEntity();
                        areaEntity.setName("全市");
                        areaEntity.setNo("");
                        areaEntity.setSimpleName("全市");
                        areaEntity.setSelected(false);
                        list.get(i).getChildren().get(j).getChildren().add(0, areaEntity);
                    }
                } else {
                    list.get(i).getChildren().add(0, provinceEntity1);
                }
            }
            String name = "";
            for (int k = 0; k < choosenCityList.size(); k++) {
                name = choosenCityList.get(k).getName();
                if (list != null && list.size() > 0) {
                    if (name.equals("全国")) {
                        for (int i = 0; i < list.size(); i++) {
                            list.get(i).setSelected(true);
                            for (int j = 0; j < list.get(i).getChildren().size(); j++) {
                                list.get(i).getChildren().get(j).setSelected(true);
                            }
                        }
                    } else {
                        for (int i = 0; i < list.size(); i++) {
                            if (list.get(i).getName().equals(name)) {
                                list.get(i).setSelected(true);
                                for (int a = 0; a < list.get(i).getChildren().size(); a++) {
                                    list.get(i).getChildren().get(a).setSelected(true);
                                }
                            } else {
                                for (int j = 0; j < list.get(i).getChildren().size(); j++) {
                                    if (list.get(i).getChildren().get(j).getName().equals(name)) {
                                        list.get(i).getChildren().get(j).setSelected(true);
                                        list.get(i).setSelected(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            mCityPop.BaseData(list, false);
            mCityPop.showPopwindowBottom(mBinding.tvConfirm);
        } else {
            mCityPop.setMultiData(cityDatas, true);
            mCityPop.showPopwindowBottom(mBinding.tvConfirm);
        }
    }
}
